clear; clc; close all

dir = 'C:\Users\Mittal\Documents\Work\Code\bxr-bayesian-regression\bin\';
prefix = 'modelfile';
m = 5;
models = {'LD','HD'};
losses = {'L1','L2'};

for model = models,
    for loss = losses,
        beta0 = [inf inf];
        beta1 = [inf inf];
        out = [dir prefix '_' model{1} '_' loss{1} '_avg.txt'];
        fp_out = fopen(out,'w');
        for i = 0:m-1,
            in = [dir prefix '_' model{1} '_' loss{1} '_' num2str(i) '.txt'];
            fprintf('%s\n',in);
            fp = fopen(in,'r');
            data = textscan(fp,'%s','Delimiter','\n','BufSize',20000000);
            data = data{1};
            fclose(fp);
            for j = 1:length(data),
                line = data{j};
                words = strread(line,'%s','delimiter',' ');
                if(strcmp(words{1},'betaClassSparse'))
                    for k = 3:length(words),
                        word = words{k};
                        colon = find(word == ':');
                        key = word(1:colon-1);
                        if(strcmp(key,'@constant'))
                            key = 0;
                        else
                            key = str2num(key);
                        end
                        value = str2num(word(colon+1:end));
                        if(strcmp(words{2},'0'))
                            key_ind = find(beta0(:,1)==key);
                            if(isempty(key_ind))
                                beta0(size(beta0,1)+1,:) = [key value];
                            else
                                beta0(key_ind,2) = beta0(key_ind,2) + value;
                            end
                        else
                            key_ind = find(beta1(:,1)==key);
                            if(isempty(key_ind))
                                beta1(size(beta1,1)+1,:) = [key value];
                            else
                                beta1(key_ind,2) = beta1(key_ind,2) + value;
                            end
                        end
                    end
                else
                    if(i == 1)
                        if(strcmp(words{1},'modelname'))
                            fprintf(fp_out,'modelname Averaged beta values\n');
                        else
                            fprintf(fp_out,'%s\n',line);
                        end
                    end
                end
            end
        end
        beta0(:,2) = beta0(:,2)/m;
        beta1(:,2) = beta1(:,2)/m;
        beta0(1,:) = [];
        beta1(1,:) = [];
        fprintf(fp_out,'betaClassSparse 0 @constant:%f',beta0(1,2));
        for k = 2:length(beta0),
            fprintf(fp_out,' %d:%f',beta0(k,1),beta0(k,2));
        end
        fprintf(fp_out,'\nbetaClassSparse 1 @constant:%f',beta1(1,2));
        for k = 2:length(beta1),
            fprintf(fp_out,' %d:%f',beta1(k,1),beta1(k,2));
        end
        fclose(fp_out);
    end
end
